﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PhotoManager
{
    public partial class RemoveDuplicateForm : Form
    {
        private string[] dupFiles = new string[0];

        public RemoveDuplicateForm()
        {
            InitializeComponent();
        }

        public string[] DuplicateFiles
        {
            get { return dupFiles; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                dupFiles = value;
                lvDupFiles.Items.Clear();
                foreach (string s in dupFiles)
                {
                    long fileSize = new FileInfo(s).Length;
                    lvDupFiles.Items.Add(s).SubItems.Add(fileSize.ToString("#,0"));
                }
            }
        }

        public bool[] DuplicateChecked
        {
            get
            {
                bool[] c = new bool[lvDupFiles.Items.Count];
                for (int i = 0; i < c.Length; i++)
                {
                    c[i] = lvDupFiles.Items[i].Checked;
                }
                return c;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (value.Length != lvDupFiles.Items.Count)
                    throw new ArgumentException("Incorrect number of elements in value.");

                for (int i = 0; i < value.Length; i++)
                {
                    lvDupFiles.Items[i].Checked = value[i];
                }
            }
        }

        public bool RememberChoice
        {
            get { return checkRemember.Checked; }
            set { checkRemember.Checked = value; }
        }

#if false
        private void listDupFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listDupFiles.IndexFromPoint(e.Location) >= 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
#endif

        private void RemoveDuplicateForm_Load(object sender, EventArgs e)
        {
            //lvDupFiles.SetWindowTheme("explorer");
            normalFont = lvDupFiles.Font;
            boldFont = new Font(normalFont, FontStyle.Bold);
            strikeoutFont = new Font(normalFont, FontStyle.Strikeout);
        }

        private void lvDupFiles_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            bool selected = lvDupFiles.CheckedIndices.Count > 0;
            btnOK.Enabled = selected;
            //checkRemember.Enabled = selected;

            foreach (ListViewItem item in lvDupFiles.Items)
            {
                if (item.Checked)
                    item.Font = boldFont;
                else
                    item.Font = normalFont;
            }
        }

        private Font normalFont;
        private Font boldFont;
        private Font strikeoutFont;

        private void btnEditRules_Click(object sender, EventArgs e)
        {
            FolderPrecedenceForm f = new FolderPrecedenceForm();
            f.ShowDialog(this);
        }

        private FolderPrecedence precedence;

        public FolderPrecedence FolderPrecedence
        {
            get { return precedence; }
            set { precedence = value; }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (lvDupFiles.SelectedIndices.Count != 1)
            {
                e.Cancel = true;
                return;
            }

            string path = lvDupFiles.SelectedItems[0].Text;

            // Remove existing menu items.
            for (int i = mnuRulesKeep.DropDownItems.Count - 1; i >= 2; i--)
                mnuRulesKeep.DropDownItems.RemoveAt(i);
            for (int i = mnuRulesRemove.DropDownItems.Count - 1; i >= 2; i--)
                mnuRulesRemove.DropDownItems.RemoveAt(i);

            while ((path = Path.GetDirectoryName(path)) != null)
            {
                ToolStripMenuItem miKeep = new ToolStripMenuItem(path);
                miKeep.CheckOnClick = true;
                miKeep.Click += mnuRulesKeepItem_Click;
                miKeep.Checked = precedence.GetFolderPriority(path) > 0;
                mnuRulesKeep.DropDownItems.Add(miKeep);

                ToolStripMenuItem miRemove = new ToolStripMenuItem(path);
                miRemove.CheckOnClick = true;
                miRemove.Click += mnuRulesRemoveItem_Click;
                miRemove.Checked = precedence.GetFolderPriority(path) < 0;
                mnuRulesRemove.DropDownItems.Add(miRemove);
            }
        }

        private void mnuRulesKeepItem_Click(object sender, EventArgs e)
        {

        }

        private void mnuRulesRemoveItem_Click(object sender, EventArgs e)
        {

        }
    }
}
